package gl;

import gl.android.biyiqing.R;
import gl.uitls.AndroidUtil;

import java.io.File;
import java.util.Stack;

import net.tsz.afinal.FinalBitmap;
import net.tsz.afinal.FinalHttp;
import android.app.Activity;
import android.app.Application;
import android.content.Context;

import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.utils.StorageUtils;

public class App extends Application {

	public static Context applicationContext;
	public static App singleton;
	public static FinalHttp mFinalhttp;
	public static FinalBitmap mFBitmap;
	@Override
	public void onCreate() {
		super.onCreate();
		singleton = this;
		mFinalhttp = new FinalHttp();
		mFBitmap = FinalBitmap.create(this);
//		mFBitmap.configLoadfailImage(R.drawable.no_media);
//		mFBitmap.configLoadingImage(R.drawable.ic_launcher);
		applicationContext = this;
		
		
		initImageLoader();
	}





	public void initImageLoader() {
		File cacheDir = StorageUtils.getCacheDirectory(getApplicationContext());
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext())
		        .memoryCacheExtraOptions(AndroidUtil.getScreenW(), AndroidUtil.getScreenH()) // default = device screen dimensions
		        .diskCacheExtraOptions(AndroidUtil.getScreenW(), AndroidUtil.getScreenH(),null)
		        .threadPoolSize(1) // default
		        .threadPriority(Thread.NORM_PRIORITY - 2) // default
		        .tasksProcessingOrder(QueueProcessingType.FIFO) // default
		        .denyCacheImageMultipleSizesInMemory()
		        .memoryCache(new LruMemoryCache(4 * 1024 * 1024))
		        .memoryCacheSize(4 * 1024 * 1024)
		        .memoryCacheSizePercentage(13) // default
		        .diskCache(new UnlimitedDiscCache(cacheDir)) // default
		        .diskCacheSize(50 * 1024 * 1024)
		        .diskCacheFileCount(100)
		        .diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
		        .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
//		        .writeDebugLogs()
		        .build();
		ImageLoader.getInstance().init(config);
	}
	private static Stack<Activity> activityStack = new Stack<Activity>();

	// Returns the application instance
	public static App getApp() {
		if (singleton == null) {
			singleton = new App();
		}
		return singleton;
	}





	public void addActivity(Activity activity) {
		activityStack.add(activity);
	}

	public Activity lastActivity() {
		Activity activity;
		try {
			activity = activityStack.lastElement();
		} catch (Exception e) {
			return null;
		}

		return activity;
	}

	public void finishActivity() {
		Activity activity = activityStack.lastElement();
		finishActivity(activity);
	}

	public void finishActivity(Activity activity) {
		if (activity != null) {
			activityStack.remove(activity);
			activity.finish();
			activity = null;
		}
	}

	public void finishActivity(Class<?> cls) {
		for (Activity activity : activityStack) {
			if (activity.getClass().equals(cls)) {
				finishActivity(activity);
			}
		}
	}

	public void finishAllActivity() {
		for (int i = 0, size = activityStack.size(); i < size; i++) {
			if (null != activityStack.get(i)) {
				activityStack.get(i).finish();
			}
		}
		activityStack.clear();
	}
	public void exit(){
		finishAllActivity();
//		stopService(new Intent(this,PushService.class));
		System.exit(0);
	}
}
